package com.sc.web.goods;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.goods.GoodsSku;
import com.sc.service.goods.GoodsSkuService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;

/**
 * 商品规格管理
 * @author maoyongbin
 * @日期 Mar 31, 2017 9:16:27 AM
 */
@Controller
@RequestMapping(value="goodsSku")
public class GoodsSkuController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private GoodsSkuService goodsSkuService;
	
	/**
	 * 根据商品id，查找商品的规格，只查规格名称和数量
	 * @param goodsId
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="selectGoodsSku")
	@ResponseBody
	public Map<String, Object> selectGoodsSku(String goodsId) {
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		if(StringUtils.isBlank(goodsId)){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
			logger.warn("查询参数有误!");
		}else{
			try{
				//查询商品数据
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("goodsId", goodsId);
				
				List<Map<String, Object>> skuMapList = this.goodsSkuService.selectGoodsSku(goodsId);
				
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, skuMapList);
				logger.info("查询商品规格数据成功!");
			}catch(Exception e){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.error("查询商品规格数据异常:"+e);
				throw new ScBizException("查询商品规格数据异常！", e);
			}
		}
		
		return resultMap;
	}
	
	/**
	 * 根据商品id，查找商品对应的规格数据
	 * @param goodsId
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="selectList")
	@ResponseBody
	public Map<String, Object> selectList(String goodsId) {
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		if(StringUtils.isBlank(goodsId)){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
			logger.warn("查询参数有误!");
		}else{
			try{
				//查询商品数据
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("goodsId", goodsId);
				
				List<GoodsSku> goodsSkuList = this.goodsSkuService.selectList(goodsId);
				
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, goodsSkuList);
				logger.info("查询商品对应规格数据成功!");
			}catch(Exception e){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.error("查询商品对应规格数据异常:"+e);
				throw new ScBizException("查询商品对应规格数据异常！", e);
			}
		}
		
		return resultMap;
	}
}
